//获取应用实例
const app = getApp();
// 引入AES.js
var baseJs = require("../../common/base.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    topData: [{
        value: "销量",
        orderBy: "SaleNumber",
        active: true
      },
      {
        value: "价格",
        orderBy: "PresentPrice",
        active: false
      },
      {
        value: "距离",
        orderBy: "Distince",
        active: false
      },
    ],
    // 
    imgUrl: baseJs.imgUrl,
    pageIndex: 0,
    state: "",
    orderBy:"SaleNumber",
    isAsc: false,
    discountId:0,
    listData:[],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var that = this;
    var discountId = this.data.discountId;
    wx.setNavigationBarTitle({
      title: options.discountName
    })
    this.setData({
      discountId:options.discountId
    });
    this.GetBuluoAreaGoodsList();
  },


  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
    this.data.pageIndex++;
    this.GetBuluoAreaGoodsList();
  },

  //最新、销量、价格 
  orderByTap: function (e) {
    var that = this;
    // console.log(e);
    var topData = this.data.topData;
    var value = e.currentTarget.dataset.value;
    var orderBy = e.currentTarget.dataset.orderby;
    var listData = this.data.listData;
    for (var i = 0, len = topData.length; i < len; ++i) {
      if (topData[i].value == value) {
        topData[i].active = true;
      } else {
        topData[i].active = false
      }
    }
    listData = [];
    this.setData({
      topData: topData,
      value: value,
      orderBy: orderBy,
      listData: listData
    })
    this.GetBuluoAreaGoodsList();
  },
  // 
  GetBuluoAreaGoodsList: function () {
    wx.showLoading({
      title: '加载中',
      icon: 'success',
      duration: 2000
    })
    var that = this;
    var orderBy = this.data.orderBy;
    var isAsc = this.data.isAsc;
    var discountId = this.data.discountId;
    var listData = this.data.listData;
    var pageIndex = this.data.pageIndex;
    wx.request({
      url: baseJs.api + baseJs.encryUrl("method=GetBuluoAreaGoodsList&PageSize=10&PageIndex=" + pageIndex + "&OrderBy=" + orderBy + "&IsAsc=" + isAsc + "&BaiduX=0" + "&BaiduY=0" + "&BuluoAreaId=" + discountId),
      success: function (res) {
        var json = JSON.parse(baseJs.decryUrl(res.data)).ResultData;
        for (var i = 0; i < json.length; i++) {
          listData.push(json[i])
        }
        that.setData({
          listData: listData,
        })
        console.log(listData);
        if (json.length == 10) {
          that.setData({
            state: 'loadMore',
          })
        } else if (json.length == 0 && pageIndex == 1) {
          that.setData({
            state: 'noData',
          })
        } else if (json.length < 10) {
          that.setData({
            state: 'noMore',
          })
        } else if (json.length == 0 && pageIndex > 1) {
          that.setData({
            state: 'noMore',
          })
        }
        wx.hideLoading();

      }
    })
  },
})